package com.stars.module.unit.controller;

import com.stars.base.entity.CommonDTO;
import com.stars.base.entity.Result;
import com.stars.module.unit.entity.StarUnitEntity;
import com.stars.module.unit.service.StarUnitService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/starUnit")
public class StarUnitController {
    private final StarUnitService unitService;

    public StarUnitController(StarUnitService unitService) {
        this.unitService = unitService;
    }

    @RequestMapping("/loadAllUnit")
    public Result loadAllDept(){
        return Result.success(unitService.loadAllUnit());
    }

    @RequestMapping("/loadUnitByFid")
    public Result loadUnitByFid(@RequestBody CommonDTO dto){
        return Result.success(unitService.loadUnitByFid(dto));
    }

    @GetMapping("/loadUnitById")
    public Result loadUnitById(String id){
        return Result.success(unitService.loadUnitById(id));
    }

    @PostMapping("/saveUnit")
    public Result saveDept(@RequestBody StarUnitEntity unit){
        try {
            return Result.result(unitService.saveOrUpdateUnit(unit));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("removeDept")
    public Result removeDept(String id){
        return Result.result(unitService.removeDept(id));
    }

    @RequestMapping("/getMaxIndex")
    public Result getMaxIndex (String fid) {
        return Result.success(unitService.getMaxIndex(fid));
    }

    /**
     * 获取根机构信息
     * @return
     */
    @RequestMapping("/getUnitRoot")
    public Result getUnitRoot(){
        return Result.success(unitService.getUnitRoot());
    }
}
